package org.display.graphic
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	
	import flex.utils.ui.resize.*;
	
	import mx.controls.*;
	import mx.managers.*;
	
	import org.data.*;
	import org.display.graphic.*;
	import org.display.graphic.line.*;
	import org.display.util.*;
	import org.utils.*;
	
	import spark.components.WindowedApplication;

	public class UmlBg extends SceneBg
	{
		private var container:Sprite
		public var bg:Sprite
		
		public function UmlBg(_areaContainer:Sprite,app:Object)
		{
			super(_areaContainer,app)
			
			this.x=0
			this.y=0
				
			bg=new Sprite()
			bg.graphics.beginFill(0x000000,0)
			bg.graphics.drawRect(0,0,Global.sceneSize,Global.sceneSize)
			bg.graphics.endFill()
				
			this.addChild(bg)
			
			
			this.removeEventListener(MouseEvent.RIGHT_MOUSE_DOWN,mouseEvent)
			
		    this.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
				
			this.addEventListener(MouseEvent.MOUSE_WHEEL,mouseEvent)
				
			//bg.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
			//bg.addEventListener(MouseEvent.MOUSE_UP,mouseEvent)
				 
			this.createMenuItems(["显示结构图","刷新","缩小","放大","清除"])
			
		}
		
		
		protected override function mouseEvent(e:MouseEvent){
			
			switch(e.type){
				
				case MouseEvent.MOUSE_DOWN:
					
					this.startDrag()
					
					break

				case MouseEvent.MOUSE_UP:
					
					this.stopDrag()
					
					break
				
				case MouseEvent.MOUSE_WHEEL:
					
					if(e.delta==-3){
						
						this.scaleY=this.scaleX+=0.1
						
						this.x=this.y=0
						
						app.umlScene.verticalScrollPosition=0
						
					}else{
						
						if(this.scaleX<0.2){return;}
						
						this.scaleY=this.scaleX-=0.1
						
						this.x=this.y=0
						
					}
					
					break
			}
		}
		

		protected override function menuselectEvent(e:ContextMenuEvent){
			
			var shape:LineBase
			
			switch(e.currentTarget.caption){
				
				case "清除":
					
					ElementControl.cleanObjByType(ShapeBase,app.umlScene)
					
					ElementControl.cleanObjByType(RotLine,app.umlScene)
					
					ElementControl.cleanObjByType(ShapeBase,this)
					
					ElementControl.cleanObjByType(RotLine,this)
					
					break
				
				case "刷新":
					
					ElementControl.cleanObjByType(ShapeBase,app.umlScene)
					
					ElementControl.cleanObjByType(RotLine,app.umlScene)
					
					
					ElementControl.cleanObjByType(ShapeBase,this)
					
					ElementControl.cleanObjByType(RotLine,this)
					
					new ShowAllXML(app.tab,app.scene,app.sceneTab,app.umlScene)
					
					break
			
				case "显示结构图":
					
					this.scaleX=this.scaleY=1
					this.x=this.y=0
					
					orderY()
					redrawLine()
					
					break
				
				case "缩小":
					
					this.scaleY=this.scaleX-=0.2
					
					break
				
				case "放大":
					
					this.scaleY=this.scaleX+=0.2
					
					break
				
			}
			
			
		}
		
		
		function createSquare(container:Iv,type:String,px:Number,py:Number){
		
		
			var square:ClassSquare=new ClassSquare(container,type)
			
			container.addChild(square)
			
			square.x = px
			square.y = py
		

		}
		
		
		function orderY(){
			
			ElementControl.cleanObjByType(Iv,app.umlScene)
			ElementControl.cleanObjByType(Iv,this)
			
			var ivs:Array=Global.jsflsPath.concat()
			
			for(var j=0;j<ivs.length;j++){
				
				ivs[j].iv=ivs[j].ivc.getIvs().join("_")
				ivs[j].ivc._roots=[]
				
			}
			
			var jsfls:Array=[]
			
			ivs=ArrayControl.orderRoot(ivs)
			
			ivs.reverse()
				
			
			
			for( j=0;j<ivs.length;j++){
				
				var container=ivs[j].ivc
					
				//trace(container.path,container._roots)
					
				//PointControl.transPos(container,this)
					
				this.addChild(container)
					
				if(container._roots.length==0){
				
					container.x=50
					container.y=50
				
				}
					
			//如果有碰撞则重新设置坐标
			var shapes:Array=ElementControl.getObjs(Iv,this)
				
			for(var  g=0;g<shapes.length;g++){		
				
				if(container.hitTestObject(shapes[g])&&container!==shapes[g]){

					container.x=shapes[g].getBounds(shapes[g].parent).x+shapes[g].getBounds(shapes[g].parent).width+40
					container.y=shapes[g].getBounds(shapes[g].parent).y

				}
				
			}	

			  //重设shapeBase的值
			   var items:Array=ElementControl.getObjs(ShapeBase,container)
					
			   for(var  k=0;k<items.length;k++){
						
					if(ShapeBase(items[k]).getEvent()!=""){
					   
						ShapeBase(items[k]).createMenuItems(["显示事件流","删除事件流"])
							
						container.addChild(ShapeBase(items[k]))
							
					}
			   }
			   
			   
			   
			   //添加view,model,viewProxy,modelProxy
			   
			  /*createSquare(container,"view",0,-40)
			   
			   createSquare(container,"model",-20,-40)
			   
			   createSquare(container,"viewProxy",-40,-20)
			   
			   createSquare(container,"modelProxy",-20,0)
			   */
				
				container.scaleX=0.5
				
				container.scaleY=0.5
				
				var disnum:Number=container.getBounds(container.parent).x
				
				for(var k=j+1;k<ivs.length;k++){
					
					var obj=ivs[k].ivc
					
					if(container.check(obj)){
						
						obj._roots.push(container)
						
						obj.scaleX=0.5
						
						obj.scaleY=0.5
						
						obj.x=disnum
						
						obj.y=container.getBounds(container.parent).y+container.getBounds(container.parent).height+50

						disnum=obj.getBounds(obj.parent).x+obj.getBounds(obj.parent).width
						
					}
					
				}
				
				
				
				
			}
			
			
		
			
		}
		
		
		function redrawLine(){
			
			ElementControl.cleanObjByType(RotLine,app.umlScene)
			ElementControl.cleanObjByType(RotLine,this)
		
			var ivs:Array=Global.jsflsPath.concat()
			
			for(var j=0;j<ivs.length;j++){
				
				ivs[j].iv=ivs[j].ivc.getIvs().join("_")
				ivs[j].ivc.rots=[]
				
			}
			
			var jsfls:Array=[]
			
			ivs=ArrayControl.orderRoot(ivs)
			
			ivs.reverse()
				
			
			for( j=0;j<ivs.length;j++){
				
				var container=ivs[j].ivc
				
			for(var k=j+1;k<ivs.length;k++){
				
				var obj=ivs[k].ivc
				
				if(container.check(obj)){
					
					var w:Number=obj.getBounds(obj.parent).width
					
					var h:Number=obj.getBounds(obj.parent).height
						
					var rot:RotLine=new RotLine(1,0x808080,new Point(obj.x+w/2,obj.y),new Point(container.x+container.getBounds(container.parent).width/2,container.y+container.getBounds(container.parent).height))
					
					rot.obj1=obj
					rot.obj2=container
						
					obj.rots.push(rot)
					container.rots.push(rot)	
						
					this.addChild(rot)
					
				}
				
			}
			}
		
		
		
		
		}
		

	}
}